package com.test.langchain4j.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 2025/8/3 17:14
 * @modifiedBy:
 * @version: 1.0
 */
@Service
public class WeatherService {
  // 和风天气开发服务 https://dev.qweather.com/
  // 替换成你自己的和风天气API密钥
  private static final String API_KEY = "1d5f3b2e141142f79fe5b4503897db03";
  // 调用的url地址和指定的城市，本案例以北京为例
  private static final String BASE_URL =
      "https://devapi.qweather.com/v7/weather/now?location=%s&key=%s";
  @Resource private RestTemplate restTemplate;

  public JsonNode getWeatherV2(String city) throws Exception {
    // 1 传入调用地址url和apikey
    String url = String.format(BASE_URL, city, API_KEY);
    // 4 RestTemplate微服务调用
    String response = restTemplate.getForObject(url, String.class);
    // 5 解析JSON响应获得第3方和风天气返回的天气预报信息
    JsonNode jsonNode = new ObjectMapper().readTree(response);
    // 6 想知道具体信息和结果请查看https://dev.qweather.com/docs/api/weather/weather-now/#response
    return jsonNode;
  }
}
